package com.lc.luzhengtang.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.PostAdapter2;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.ShopDetailBean;
import com.lc.luzhengtang.conn.ConnShopDetail;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/19.
 */
public class CommentAllActivity2 extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {


    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.text_title)
    TextView textTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    PostAdapter2 classfiyGridAdapter;
    public int current_page = 1;
    //评价列表的list
    List<ShopDetailBean.DataBeanX.EvaluateBean.DataBean> dataBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commentall);
        ButterKnife.bind(this);
        dataBeanList = new ArrayList<>();
        textTitle.setText("商品评价");
//        getData();
        initData();
    }

    protected void initData() {
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        classfiyGridAdapter = new PostAdapter2(null);
        classfiyGridAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        classfiyGridAdapter.isFirstOnly(false);
   /*     Drawable drawable = ContextCompat.getDrawable(CommentAllActivity2.this, R.drawable.line_divider);
        recyclerView.addItemDecoration(new SimpleDividerItemDecoration(CommentAllActivity2.this, drawable, 1));*/
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(classfiyGridAdapter);

        refreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        refreshLayout.setOnRefreshListener(this);
        classfiyGridAdapter.setOnLoadMoreListener(this);

        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
    }


    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }

    public void getData() {


    }


    @OnClick({R.id.img_news, R.id.text_title})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.text_title:
                break;
        }
    }

    @Override
    public void onRefresh() {
        new ConnShopDetail(getIntent().getStringExtra("shangpin_id"), BaseApplication.basePreferences.readUid(), 1, new AsyCallBack<ShopDetailBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
                classfiyGridAdapter.setEnableLoadMore(false);
                refreshLayout.setRefreshing(true);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, ShopDetailBean shopDetailBean) throws Exception {
                super.onSuccess(toast, type, o, shopDetailBean);
            /*    classfiyGridAdapter = new PostAdapter2(shopDetailBean.getData().getEvaluate().getData());
                mDatas2 = shopDetailBean.getData().getEvaluate().getData();
                rvPostList.setAdapter(postAdapter2);*/
                dataBeanList = shopDetailBean.getData().getEvaluate().getData();
                if (dataBeanList != null) {
                    current_page = 1;
                    classfiyGridAdapter.setNewData(dataBeanList);
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                dismissLoading();
                refreshLayout.setRefreshing(false);
                classfiyGridAdapter.setEnableLoadMore(false);
            }
        }


        ).execute(CommentAllActivity2.this, false);
    }

    @Override
    public void onLoadMoreRequested() {
        new ConnShopDetail(getIntent().getStringExtra("shangpin_id"), BaseApplication.basePreferences.readUid(), 1, new AsyCallBack<ShopDetailBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
                classfiyGridAdapter.setEnableLoadMore(true);
                refreshLayout.setRefreshing(false);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, ShopDetailBean shopDetailBean) throws Exception {
                super.onSuccess(toast, type, o, shopDetailBean);
            /*    classfiyGridAdapter = new PostAdapter2(shopDetailBean.getData().getEvaluate().getData());
                mDatas2 = shopDetailBean.getData().getEvaluate().getData();
                rvPostList.setAdapter(postAdapter2);*/
                dataBeanList = shopDetailBean.getData().getEvaluate().getData();


                if (dataBeanList != null && dataBeanList.size() > 0) {
                    current_page++;
                    classfiyGridAdapter.setNewData(dataBeanList);
                } else {
                    //显示没有更多数据
                    // newsAdapter.loadComplete();
                    classfiyGridAdapter.loadMoreComplete();
                    View noDataView = LayoutInflater.from(CommentAllActivity2.this).inflate(R.layout.item_no_data, (ViewGroup) recyclerView.getParent(), false);
                    classfiyGridAdapter.addFooterView(noDataView);
                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                dismissLoading();
                refreshLayout.setRefreshing(false);
                //dismissLoading();
                classfiyGridAdapter.removeAllFooterView();
                //最后调用结束刷新的方法
                setRefreshing(false);
            }
        }


        ).execute(CommentAllActivity2.this, false);
    }
}
